package handler

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"post-hub/internal/dto"
	"post-hub/internal/model"
	"post-hub/response"
)

func DelLike(c *gin.Context) {
	var like model.Like
	err := c.ShouldBind(&like)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	var post model.Post
	err = c.ShouldBind(&post)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	err = dto.DelLike(&like, like.UserId, like.PostId)
	if err != nil {
		response.Error(c, "取消失败")
		return
	}
	response.Success(c, "取消成功", like)
	post = model.Post{
		Model: gorm.Model{
			ID: uint(like.PostId),
		},
		Likes: post.Likes - 1,
	}
	dto.PostUpdate(&post, int(post.ID))
}
